package com.xiaoshuidi.cloud.module.rooms.controller.admin.apartment;


import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.apartment.vo.*;
import com.xiaoshuidi.cloud.module.rooms.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.rooms.service.ApartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Tag(name = "管理后台 - 门店管理")
@RestController
@RequestMapping(ApiConstants.NAME + "/apartment")
public class ApartmentController {

    @Resource
    private ApartmentService apartmentService;

    @PostMapping("/create")
    @Operation(summary = "创建门店")
    @Parameters({
            @Parameter(name = "deptId", description = "部门id"),
            @Parameter(name = "apartmentName", description = "门店名")
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:create')")
    public CommonResult<Long> create(@RequestParam("deptId") Long deptId, @RequestParam("apartmentName") String apartmentName) {
        Long tenantId = SecurityFrameworkUtils.getLoginUser().getTenantId();
        return apartmentService.create(deptId, apartmentName, tenantId);
    }

    @PutMapping("/edit")
    @Operation(summary = "编辑门店")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:edit')")
    public CommonResult<Boolean> edit(@Valid @RequestBody ApartmentEditVO editVO) {
        return apartmentService.edit(editVO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门店")
    @Parameter(name = "apartmentId", description = "门店id")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:delete')")
    public CommonResult<Boolean> delete(@RequestParam("apartmentId") Long apartmentId) {
        return apartmentService.delete(apartmentId);
    }

    @DeleteMapping("/delete-by-deptId")
    @Operation(summary = "根据部门删除门店")
    @Parameter(name = "deptId", description = "部门ID", example = "1024", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:delete')")
    public CommonResult<Boolean> deleteByDeptId(@RequestParam("deptId") Long deptId){
        return apartmentService.deleteByDeptId(deptId);
    };

    @GetMapping("/checkDelete")
    @Operation(summary = "校验门店是否可以删除")
    @Parameter(name = "apartmentId", description = "门店id", example = "1024", required = true)
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:delete')")
    public CommonResult<Boolean> checkDelete(@RequestParam("apartmentId") Long apartmentId) {
        return apartmentService.checkDelete(apartmentId);
    }

    @GetMapping("/detail")
    @Parameter(name = "apartmentId", description = "门店id", example = "1024", required = true)
    @Operation(summary = "查看门店详情")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:query')")
    public CommonResult<ApartmentVO> detail(@RequestParam("apartmentId") Long apartmentId) {
        return apartmentService.detail(apartmentId);
    }

    @GetMapping("/pageList")
    @Parameters({
            @Parameter(name = "pageSize", description = "每页大小"),
            @Parameter(name = "pageNum", description = "第几页"),
            @Parameter(name = "name", description = "门店名"),
            @Parameter(name = "apartmentId", description = "门店id"),
            @Parameter(name = "deptId", description = "部门id"),
    })
    @Operation(summary = "门店分页")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:query')")
    public CommonResult<PageResult<ApartmentVO>> pageList(Integer pageSize, Integer pageNum, String name, Long apartmentId) {
        return apartmentService.apartmentPageList(pageSize, pageNum, name, apartmentId);
    }

    @GetMapping("/list")
    @Operation(summary = "获取部门所有门店")
    @Parameters({
            @Parameter(name = "apartmentName", description = "门店名"),
            @Parameter(name = "isConcentrated", description = "是否集中式", example = "true:集中式 false:分散式"),
    })
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:query')")
    public CommonResult<List<ApartmentVO>> list(String apartmentName, Boolean isConcentrated) {
        return apartmentService.myList(apartmentName, isConcentrated);
    }

    @PutMapping("/edit-apartment-name")
    @Parameters({
            @Parameter(name = "deptId", description = "部门id"),
            @Parameter(name = "name", description = "新的门店名"),
    })
    @Operation(summary = "编辑门店名称")
    public CommonResult<Long> editApartmentName(@RequestParam("deptId") Long deptId,@RequestParam("name") String name){
        return apartmentService.editApartmentName(deptId, name);
    }
//********************************************************************************************************************************

    @GetMapping("/getMap")
    @Operation(summary = "获取地图数据")
    public CommonResult<List<ApartmentMapVO>> getMap(){
        return CommonResult.success(apartmentService.getMap());
    }

    @PostMapping("/getApartments")
    @Operation(summary = "获取地图门店列表")
    @Parameter(name = "apartmentIds", description = "门店编号列表", required = false)
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:query')")
    public CommonResult<List<ApartmentRespVO>> getApartments(@RequestParam("apartmentIds") List<Long> apartmentIds){
        return CommonResult.success(apartmentService.getApartments(apartmentIds));
    }

    @PostMapping("/updateLongitudeAndLatitude")
    @Operation(summary = "批量更新门店经纬度（高德api）")
    public CommonResult<Boolean> updateLongitudeAndLatitude(){
        apartmentService.updateLongitudeAndLatitude();
        return CommonResult.success(true);
    }

    @Operation(summary = "分散式房源获取门店详情")
    @Parameters({
            @Parameter(name = "roomHouseId",description = "房间id"),
    })
    @GetMapping("/distributedDetail")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:apartment:query')")
    public CommonResult<DistributedDetailRespVO> distributedDetail(@RequestParam("roomHouseId") Long roomHouseId){
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return apartmentService.distributedDetail(roomHouseId, loginUserId);
    }

}
